Imports KanBan.Pgd.Infrastructure.Exceptions
Imports KanBan.Pgd.Infrastructure.EventLog
Imports KanBan.Pgd.Presentation.Helpers
Imports KanBan.Pgd.Common
Imports KanBan.Pgd.Infrastructure.Repositories
Imports KanBan.Pgd.Presentation.Session
Imports KanBan.Pgd.Domain.Entities
Imports System.Threading
Imports System.Resources
Imports KanBan.Pgd.Security
Imports KanBan.Pgd.Domain.ValueObjects

Public Class Login

    Public Event UserIsLogged()

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        AddHandler ClientSession.ApplicationCultureChanged, AddressOf OnApplicationCultureChanged

        Dim cultures = PgdSettings.GetSupportedApplicationCultures()
        Me.AddCultures(cultures)

    End Sub

#Region "ILoginView"

    Public ReadOnly Property UserName() As String
        Get
            Return txtUserName.Text
        End Get
    End Property

    Public ReadOnly Property Password() As String
        Get
            Return txtPassword.Text
        End Get
    End Property

    Public ReadOnly Property Language() As String
        Get
            Return cmbCultures.SelectedValue.ToString()
        End Get
    End Property

    Public Sub AddCultures(ByVal cultures As IEnumerable(Of ApplicationCulture))

        cmbCultures.ValueMember = "Name"
        cmbCultures.DisplayMember = "DisplayName"

        For Each ac As ApplicationCulture In cultures
            cmbCultures.Items.Add(ac)
            If ac.IsDefault Then cmbCultures.SelectedItem = ac
        Next

    End Sub

#End Region

    Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
        If Me.Login() Then
            RaiseEvent UserIsLogged()
            Me.Close()
        Else
            Dim errorMessage As New MessageDialog(MessageDialog.MessageDialogType.ErrorMessage, _
                                                  "Error while Login")
            errorMessage.Show()

        End If
    End Sub

    Public Function Login() As Boolean

        Dim userRepository As New UserRepository()

        Dim userName As String = Me.UserName
        Dim password As String = Encryption.EncryptMD5(Me.Password)

        Dim user As User = userRepository.GetByUserName(userName)

        If user Is Nothing Then Return False

        If user.IsCorrupted Then
            Throw New ApplicationException("The User Account you are trying to log in with has been modified outside the environment of the application. Contact the Administrator.")
        End If

        If password <> user.Password Then Return False

        Dim userPermissions As IList(Of Permission) = userRepository.GetAllPermissions(user.Id)

        ClientSession.UserCredentials = New UserCredentials(user.Id, user.UserName, userPermissions)

        EventLogger.Log(String.Format("User {0} has logged in successfully", user.UserName))

        Return True

    End Function


    Private Sub OnApplicationCultureChanged()

        Thread.CurrentThread.CurrentCulture = ClientSession.CurrentCulture
        Thread.CurrentThread.CurrentUICulture = ClientSession.CurrentCulture

        Dim th As New UITranslationHelper(New ResourceManager(GetType(Login)))
        th.Translate(Me.Controls)

    End Sub

    Private Sub cmbCultures_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbCultures.SelectedIndexChanged

        Dim selectedCulture As ApplicationCulture = CType(cmbCultures.SelectedItem, ApplicationCulture)

        ClientSession.CurrentCulture = selectedCulture

    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub txtUserName_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtUserName.KeyPress

        If e.KeyChar = ChrW(Keys.Enter) Then
            Login()
        End If

    End Sub

    Private Sub txtPassword_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPassword.KeyPress

        If e.KeyChar = ChrW(Keys.Enter) Then
            Login()
        End If

    End Sub

End Class